/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief  Recursive-Descent-Function of non-terminal initializer.
 *
 * @author  WXQ#XDU
 * @date    2024.12
 */

#include "../_parser_inner.h"

#define THIS_NT_ID  initializer

// initializer                 //------------------ LL(1) production 7,8
//    :  additiveExpression     // LA(1) is not '{'
//    |  '{'
//           ( initializer ( ','  initializer )* )?
//       '}'
//
t_tree_ptr RDF_initializer (t_parser_ptr pThis) {
    int productionId = PRD_ID_UNKNOWN;
    t_tree_ptr root = tree_new_parent(THIS_NT, productionId);
    enter_nonterminal ( pThis, THIS_NT, root );
    CHECK_NULLTREE_ABORT ( root, root );
// -------  BEGIN of local action

    t_tree_ptr subTree = NULL;
    if ( TK_LBRACE != LOOK_AHEAD(pThis) ) // initializer for a scalar
    {
        productionId = 7;
        subTree = RDF_additiveExpression(pThis);
        CHECK_NULLTREE_ABORT (subTree, root);
        tree_add_child(root, subTree);
    }
    else    // initializer for an array
    {
        productionId = 8;
        subTree = parser_match(pThis, TK_LBRACE);  // consume '{'
        CHECK_NULLTREE_ABORT (subTree, root);
        tree_add_child(root, subTree);

        // optional initializer list, its length is 0, 1 or more
        if ( TK_RBRACE != LOOK_AHEAD(pThis) ) {   // LA(1) is not '}'
            subTree = RDF_initializer(pThis); // analysis the first initializer
            CHECK_NULLTREE_ABORT (subTree, root);
            tree_add_child(root, subTree);

            // expand the construct:   (','  initializer)*
            while ( TK_COMMA == LOOK_AHEAD(pThis) ) {
                subTree = parser_match(pThis, TK_COMMA);
                CHECK_NULLTREE_ABORT (subTree, root);
                tree_add_child(root, subTree);

                // analysis the 2nd, 3rd, 4th, ..., and Nth initializer
                subTree = RDF_initializer(pThis);
                CHECK_NULLTREE_ABORT (subTree, root);
                tree_add_child(root, subTree);
            }
        }

        subTree = parser_match(pThis, TK_RBRACE);   // consume '}'
        CHECK_NULLTREE_ABORT (subTree, root);
        tree_add_child(root, subTree);
    }

// -------  END of local action
    if (PRD_ID_UNKNOWN == productionId) {
        ERROR_REPORT_BUG("Forgot to correct the value of "
                         "productionId in RDF_" NT_TEXT "()");
    }
    tree_reset_production( root, productionId );

LABEL_EXIT_RDF:
    exit_nonterminal  ( pThis, THIS_NT, root );
    return root;
}

